<!-- 商品审批 -->
<template>
  <el-container>
    <el-aside width="170px">
      <CategoryManager :config="config" />
    </el-aside>
    <el-main class="base_main_content">
      <div class="base_screen_header">
        <span></span>
        <el-form :inline="true" class="base_search_form" @submit.prevent>
          <el-form-item>
            <el-select v-model="base" @change="searchBase" placeholder="请选择基地" clearable>
              <el-option v-for="(item, index) in baseOptions" :key="index" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="name" @keyup[native].enter="searchName" @change="searchName" placeholder="商品名称" clearable
              :suffix-icon="Search" />
          </el-form-item>
        </el-form>
      </div>
      <StatusBar :data="statusList" default="all" />
      <div class="base_main_margin_top">
        <ExamineTable :type="config.type" :data="data" :total="total" @getData="getList" />
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import CategoryManager from '@com/CategoryManager.vue';
import StatusBar from '@com/StatusBar.vue';
import ExamineTable from '@com/examine/ExamineTable.vue'
import { Search } from '@element-plus/icons-vue'
import useRoute from "@util/router";
import { ref, reactive } from 'vue'
import { $msg, $box } from "@ui/msg"
import api from "@api/examine"

const config = {
  type: 'goods'
}

const route = useRoute()
const data = ref([])
const total = ref(0)

route.watchRouter('goodsexamine', () => {
  getStatistics()
  getList()
})

const baseOptions = ref([])

getConfig()
function getConfig() {
  api.base(val => {
    baseOptions.value = val
  })
}

const statusList = reactive([{
  label: '全部',
  value: 'all',
  total: 0
}, {
  label: '待审批',
  value: '1007002',
  total: 0
}, {
  label: '已通过',
  value: '1007004',
  total: 0
}, {
  label: '已驳回',
  value: '1007003',
  total: 0
}])

const base = ref(route.query().base || '')
const name = ref(route.query().name || '')

function searchBase(val) {
  route.replaceQuery('base', val)
}

function searchName(val) {
  route.replaceQuery('name', val)
}

getList()
function getList() {
  let routeQuery = route.query()
  api.list(config.type, {
    classify: routeQuery.c,
    name: routeQuery.name,
    flowStatus: routeQuery.status,
    shopCode: routeQuery.base,
    page: routeQuery.p,
    size: routeQuery.s
  }, val => {
    total.value = val.total
    data.value = val.list.map(e => {
      // 1007001草稿 1007002待审批 1007003被驳回 1007004审批通过
      switch (e.flowStatus) {
        case "1007001":
          e.state = "草稿"
          break
        case "1007002":
          e.state = "待审批"
          break
        case "1007003":
          e.state = "已驳回"
          break
        case "1007004":
          e.state = "已通过"
          break
      }
      return e
    })
  })
}

getStatistics()
function getStatistics() {
  let routeQuery = route.query()
  api.num(config.type, routeQuery.c, routeQuery.base, routeQuery.name, val => {
    statusData(val)
  })
}

function statusData(val) {
  statusList.forEach((e, i) => { e.total = val[i] })
}

</script>